﻿Public Class LapMessage
    Inherits MessageBase

    Public Sub New()
        mRecords = New List(Of RecordMessage)
    End Sub

    Public Property MessageIndex As Integer?
    Public Property Timestamp As Date?
    Public Property [Event] As EventEnum?
    Public Property EventType As EventTypeEnum?
    Public Property StartTime As Date?
    Public Property StartPositionLatitude As Integer?
    Public Property StartPositionLongitude As Integer?
    Public Property EndPositionLongatude As Integer?
    Public Property EndPositionLatitude As Integer?
    Public Property TotalElapsedTime As Decimal?
    Public Property TotalTimerTime As Decimal?
    Public Property TotalDistance As Decimal?
    Public Property TotalCycles As UInt32?
    Public Property TotalStrides As UInt32?
    Public Property TotalCalories As Integer?
    Public Property TotalFatCalories As Integer?
    Public Property AvgSpeed As Decimal?
    Public Property MaxSpeed As Decimal?
    Public Property AvgHeartRate As Integer?
    Public Property MaxHeartRate As Integer?
    Public Property AvgCadence As UInt32?
    Public Property AvgRunningCadence As UInt32?
    Public Property MaxCadence As UInt32?
    Public Property MaxRunningCadence As UInt32?
    Public Property AvgPower As Integer?
    Public Property MaxPower As Integer?
    Public Property TotalAscent As Integer?
    Public Property TotalDescent As Integer?
    Public Property Intensity As IntensityEnum?
    Public Property LapTrigger As LapTriggerEnum?
    Public Property Sport As SportEnum?
    Public Property EventGroup As Integer?

    Protected Overrides Sub Populate(reader As System.IO.BinaryReader, Definition As DefinitionMessage)
        For Each FieldDef As FieldDefinition In Definition.Fields
            Dim size As Integer = FieldDef.Size
            Dim FieldDefNumber As Integer = FieldDef.FieldDefinitionNumber
            Dim FieldType As Integer = FieldDef.BaseType
            Select Case FieldDef.FieldDefinitionNumber
                Case 254 ' message_index(message_index)
                    MessageIndex = reader.ReadUInt16
                Case 253 ' timestamp(date_time)
                    Timestamp = FitHelper.TranslateDateTime(reader.ReadUInt32)
                Case 0 'event	event
                    [Event] = CType(reader.ReadByte, EventEnum)
                Case 1 ' event_type(event_type)
                    EventType = CType(reader.ReadByte, EventTypeEnum)
                Case 2 ' start_time(date_time)
                    StartTime = FitHelper.TranslateDateTime(reader.ReadUInt32)
                Case 3 ' start_position_lat(sint32)
                    StartPositionLatitude = reader.ReadInt32
                Case 4 ' start_position_long(sint32)
                    StartPositionLongitude = reader.ReadInt32
                Case 5 ' end_position_lat(sint32)
                    EndPositionLatitude = reader.ReadInt32
                Case 6 ' end_position_long(sint32)
                    EndPositionLongatude = reader.ReadInt32
                Case 7 ' total_elapsed_time(UInt32)
                    TotalElapsedTime = CDec(reader.ReadUInt32 / 1000)
                Case 8 ' total_timer_time(UInt32)
                    TotalTimerTime = CDec(reader.ReadUInt32 / 1000)
                Case 9 ' total_distance(UInt32)
                    TotalDistance = CDec(reader.ReadUInt32 / 100)
                Case 10 ' total_cycles(UInt32), total_strides(UInt32)
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        TotalStrides = reader.ReadUInt32
                    Else
                        TotalCycles = reader.ReadUInt32
                    End If
                Case 11 ' total_calories(UInt16)
                    TotalCalories = reader.ReadUInt16
                Case 12 ' total_fat_calories(UInt16)
                    TotalFatCalories = reader.ReadUInt16
                Case 13 ' avg_speed(UInt16)
                    AvgSpeed = CDec(reader.ReadUInt16 / 1000)
                Case 14 ' max_speed(UInt16)
                    MaxSpeed = CDec(reader.ReadUInt16 / 1000)
                Case 15 ' avg_heart_rate(uint8)
                    AvgHeartRate = reader.ReadByte
                Case 16 ' max_heart_rate(uint8)
                    MaxHeartRate = reader.ReadByte
                Case 17 ' avg_cadence(uint8), avg_running_cadence(uint8)
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        AvgCadence = reader.ReadByte
                    Else
                        AvgRunningCadence = reader.ReadByte
                    End If
                Case 18 ' max_cadence(uint8), max_running_cadence(uint8)
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        MaxCadence = reader.ReadByte
                    Else
                        MaxRunningCadence = reader.ReadByte
                    End If
                Case 19 ' avg_power(UInt16)
                    AvgPower = reader.ReadUInt16
                Case 20 ' max_power(UInt16)
                    MaxPower = reader.ReadUInt16
                Case 21 ' total_ascent(UInt16)
                    TotalAscent = reader.ReadUInt16
                Case 22 ' total_descent(UInt16)
                    TotalDescent = reader.ReadUInt16
                Case 23 'intensity(intensity)
                    Intensity = CType(reader.ReadByte, IntensityEnum)
                Case 24 ' lap_trigger(lap_trigger)
                    LapTrigger = CType(reader.ReadByte, LapTriggerEnum)
                Case 25 ' sport(sport)
                    Sport = CType(reader.ReadByte, SportEnum)
                Case 26 ' event_group(uint8)
                    EventGroup = reader.ReadByte
                Case Else
                    FitHelper.ReadUnknownField(reader, FieldDef)
            End Select
        Next
    End Sub

    Protected mRecords As List(Of RecordMessage)
    Public ReadOnly Property Records As IList(Of RecordMessage)
        Get
            Return mRecords.AsReadOnly
        End Get
    End Property

    Public Sub AddRecord(Record As RecordMessage)
        mRecords.Add(Record)
    End Sub
End Class
